/**
 * 提供updateFile更新iconfont静态资源文件方法，
 * url：iconfont官网提供的项目 Font class地址
 * path 需要存入的文件夹路径 default："C:\\Users\\txm\\Desktop\\nodejs\\integrated-app\\src\\assets\\iconfont"
 * name 文件名 default: "iconfont.css"
 */
const axios = require('axios').default;
const dayjs = require('dayjs');
axios.defaults.headers.common = {
    Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0'
}

const fs = require('fs')

function updateFile(url, path, name) {
    let index = url.indexOf("f")
    const baseUrl = "https:" + url.slice(0, index)
    // console.log(url.slice(index));
    const obj = {
        spm: 'a313x.manage_type_myprojects.i1.9.6e893a81ip0HrR',
        file: url.slice(index)
    }
    const filePath = path || "C:\\Users\\txm\\Desktop\\nodejs\\integrated-app\\src\\assets\\iconfont";
    const fileName = name || 'iconfont.css'
    axios.get(baseUrl + `${obj.file}`, {
        params: obj
    }).then(res => {
        // console.log(res.data);
        let fStr = `/*上次更新时间：${dayjs().format("YYYY-MM-DD HH:mm:ss").toString()}\n脚本路径：${__filename}*/\n`
        fs.writeFile(filePath + `\\${fileName}`, fStr + res.data, (err) => {
            if (err) {
                return console.log("err", err)
            }
            console.log({ filePath: filePath + `\\${fileName}`, message: '文件写入成功。' })
        })
    })
}

updateFile('//at.alicdn.com/t/c/font_4332923_jdbcj6jx8is.css')